package com.kbbywyclient.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.ResultInfoCre;
import com.kbbywyclient.ui.adapter.base.BaseLvAdapter;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by FHW
 * on 2017/4/10
 */

public class BoughtTableCreditAdapter extends BaseLvAdapter<ResultInfoCre> {
    static final int TYPE_COOPERATION=2;

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_education)
        TextView tv_education;
        @BindView(R.id.tv_occupation)
        TextView tv_occupation;
        @BindView(R.id.tv_credit)
        TextView tv_credit;
        @BindView(R.id.iv_buy)
        ImageView iv_buy;
        @BindView(R.id.tv_time)
        TextView tv_time;

        @BindView(R.id.tv_contact)
        TextView tv_contact;
        @BindView(R.id.tv_mobile)
        TextView tv_mobile;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1{
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_like_card)
        TextView tv_like_card;
        @BindView(R.id.ll_have_card)
        LinearLayout ll_have_card;
        @BindView(R.id.tv_credit)
        TextView tv_credit;
        @BindView(R.id.tv_time)
        TextView tv_time;

        @BindView(R.id.tv_contact)
        TextView tv_contact;
        @BindView(R.id.tv_mobile)
        TextView tv_mobile;

        public ViewHolder1(View view){
            ButterKnife.bind(this,view);
        }
    }

    public BoughtTableCreditAdapter(Context context, List<ResultInfoCre> list) {
        super(context, 0, list);
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType=getItemViewType(position);
        if (viewType == TYPE_NO_DATA) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.layout_data_error, parent,false);
            }
        } else if(viewType==TYPE_NORMAL){
            //非总行(合作银行)
            ResultInfoCre resultInfo = getItem(position);

            ViewHolder holder ;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_bought_credit_table,parent,false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            ViewUtils.setText(holder.tv_name, resultInfo.getUserName());

            if (StringUtil.isNotEmpty(resultInfo.getHighestEducation())) {
                holder.tv_education.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_education, resultInfo.getHighestEducation());
            }
            if (StringUtil.isNotEmpty(resultInfo.getUnitNature())) {
                holder.tv_occupation.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_occupation, resultInfo.getUnitNature());
            }
            if (StringUtil.isNotEmpty(resultInfo.getCreditCard())) {
                holder.tv_credit.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_credit, resultInfo.getCreditCard());
            }

            ViewUtils.setText(holder.tv_contact, resultInfo.getStatusName());
            ViewUtils.setText(holder.tv_mobile, resultInfo.getMobile());

            ViewUtils.setText(holder.tv_time, resultInfo.getApplyDateTime());

            holder.tv_contact.setOnClickListener(v -> {
                if (!resultInfo.getStatusName().equals("已办理") && !resultInfo.getStatusName().equals("已联系条件不符")
                        && !resultInfo.getStatusName().equals("已上门条件不符") && !resultInfo.getStatusName().equals("已查阅条件不符")) {
                    EventBus.getDefault().post(new Event.CreditCardModify(position));
                }
            });
            holder.tv_mobile.setOnClickListener(v -> {
                ViewUtils.dialPhoneNumber(getContext(), resultInfo.getMobile());
                EventBus.getDefault().post(new Event.BoughtMobile(position));
            });
        }else{
            //总行(合作银行)
            ResultInfoCre resultInfo = getItem(position);

            ViewHolder1 holder ;
            if (convertView == null) {
                convertView =  mInflater.inflate(R.layout.item_bought_credit_table_cooperation,parent,false);
                holder = new ViewHolder1(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder1) convertView.getTag();
            }

            String userName=resultInfo.getUserName();
            if(TextUtils.isEmpty(userName)){
                ViewUtils.setText(holder.tv_name, resultInfo.getMobile());
            }else{
                ViewUtils.setText(holder.tv_name,userName+"  "+resultInfo.getMobile());
            }
            String likeCardName=resultInfo.getCardName();
            if(TextUtils.isEmpty(likeCardName)){
                holder.tv_like_card.setVisibility(View.GONE);
            }else{
                holder.tv_like_card.setText("喜欢的卡:\n"+likeCardName);
                holder.tv_like_card.setVisibility(View.VISIBLE);
            }

            String haveCard=resultInfo.getCreditCard();
            if(TextUtils.isEmpty(haveCard)){
                holder.ll_have_card.setVisibility(View.GONE);
            }else{
                holder.tv_credit.setText(haveCard);
                holder.tv_credit.setText(haveCard);
            }

            ViewUtils.setText(holder.tv_contact, resultInfo.getStatusName());
            ViewUtils.setText(holder.tv_mobile, resultInfo.getMobile());

            ViewUtils.setText(holder.tv_time, resultInfo.getApplyDateTime());

            holder.tv_contact.setOnClickListener(v -> {
                if (!resultInfo.getStatusName().equals("已办理") && !resultInfo.getStatusName().equals("已联系条件不符")
                        && !resultInfo.getStatusName().equals("已上门条件不符") && !resultInfo.getStatusName().equals("已查阅条件不符")) {
                    EventBus.getDefault().post(new Event.CreditCardModify(position));
                }
            });

            holder.tv_mobile.setOnClickListener(v -> {
                ViewUtils.dialPhoneNumber(getContext(), resultInfo.getMobile());
                EventBus.getDefault().post(new Event.BoughtMobile(position));
            });
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if(mList==null||mList.size()==0){
            return TYPE_NO_DATA;
        }
        return "0".equals(getItem(position).getBankCenterDisplay())?TYPE_NORMAL:TYPE_COOPERATION;
    }

    @Override
    public int getCount() {
        return mList==null||mList.size()==0?1:mList.size();
    }
}
